package org.libarae.communication.common;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class MessageThread extends Common {
	private int mThreadId;
	private int mUser1;
	private String mUserName1 = "";
	private int mUser2;
	private String mUserName2 = "";
	private String mSubject = "";
	private int mBid = -1;
	private long mTime = 0;
	private boolean mHasUnRead = false;

	
	public MessageThread(){
		super();	
	}
	
	public MessageThread(int threadId, int user1, String userName1, int user2, String userName2, String subject, int bid, long time, boolean hasUnRead){
		super();
		setThreadId(threadId);
		setUser1(user1);
		setUserName1(userName1);
		setUser2(user2);
		setUserName2(userName2);
		setSubject(subject);
		setBid(bid);
		setTime(time);
		setHasUnRead(hasUnRead);
	}

	@Override
	public void decodeXML(Node root) {
		NodeList nodeList = root.getChildNodes();

		// set all of the nodes in their proper places
		for (int i = 0; i < nodeList.getLength(); i++) {
			// find what node we have
			String nodeName = nodeList.item(i).getNodeName();
			if (nodeName.equals("mThreadId")) {
				mThreadId = Integer.parseInt(nodeList.item(i).getTextContent());
			}
			else if (nodeName.equals("mUser1")) {
				mUser1 = Integer.parseInt(nodeList.item(i).getTextContent());
			}
			else if (nodeName.equals("mUserName1")) {
				mUserName1 = nodeList.item(i).getTextContent();
			}
			else if (nodeName.equals("mUser2")) {
				mUser2 = Integer.parseInt(nodeList.item(i).getTextContent());
			}
			else if (nodeName.equals("mUserName2")) {
				mUserName2 = nodeList.item(i).getTextContent();
			}
			else if (nodeName.equals("mSubject")) {
				mSubject = nodeList.item(i).getTextContent();
			}
			else if (nodeName.equals("mTime")) {
				mTime = Long.parseLong(nodeList.item(i).getTextContent());
			}
			else if (nodeName.equals("mBid")) {
				mBid = Integer.parseInt(nodeList.item(i).getTextContent());
			}
			else {
				System.err.println("Unknow field in object XML: " + nodeName);
			}
		}		
	}
	
	
	@Override
	public void createXml(Document doc, Element root) {
		Element node = doc.createElement("mThreadId");
		node.setTextContent(mThreadId+"");
		root.appendChild(node);
			
		node = doc.createElement("mUser1");
		node.setTextContent(mUser1+"");
		root.appendChild(node);
		
		node = doc.createElement("mUserName1");
		node.setTextContent(mUserName1);
		root.appendChild(node);
		
		node = doc.createElement("mUser2");
		node.setTextContent(mUser2+"");
		root.appendChild(node);
		
		node = doc.createElement("mUserName2");
		node.setTextContent(mUserName2);
		root.appendChild(node);
		
		node = doc.createElement("mSubject");
		node.setTextContent(mSubject);
		root.appendChild(node);
		
		node = doc.createElement("mBid");
		node.setTextContent(mBid+"");
		root.appendChild(node);
		
		node = doc.createElement("mTime");
		node.setTextContent(mTime+"");
		root.appendChild(node);	
	}

	public int getThreadId() {
		return mThreadId;
	}

	public void setThreadId(int mThreadId) {
		this.mThreadId = mThreadId;
	}

	public int getUser1() {
		return mUser1;
	}

	public void setUser1(int mUser1) {
		this.mUser1 = mUser1;
	}

	public String getUserName1() {
		return mUserName1;
	}

	public void setUserName1(String mUserName1) {
		this.mUserName1 = mUserName1;
	}

	public int getUser2() {
		return mUser2;
	}

	public void setUser2(int mUser2) {
		this.mUser2 = mUser2;
	}

	public String getUserName2() {
		return mUserName2;
	}

	public void setUserName2(String mUserName2) {
		this.mUserName2 = mUserName2;
	}

	public String getSubject() {
		return mSubject;
	}

	public void setSubject(String mSubject) {
		this.mSubject = mSubject;
	}

	public int getBid() {
		return mBid;
	}

	public void setBid(int mBid) {
		this.mBid = mBid;
	}
	
	public long getTime() {
		return mTime;
	}

	public void setTime(long mTime) {
		this.mTime = mTime;
	}
	
	public boolean isHasUnRead() {
		return mHasUnRead;
	}

	public void setHasUnRead(boolean mHasUnRead) {
		this.mHasUnRead = mHasUnRead;
	}
}
